package org.xiaojl.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xiaojl.sys.entity.SysResource;
import org.xiaojl.sys.entity.SysRole;
import org.xiaojl.sys.entity.SysRoleResource;
import org.xiaojl.sys.mapper.SysRoleMapper;
import org.xiaojl.sys.service.SysResourceService;
import org.xiaojl.sys.service.SysRoleResourceService;
import org.xiaojl.sys.service.SysRoleService;
import org.xiaojl.utils.ToolUtil;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

/**
 * <p>
 * 系统角色 服务实现类
 * </p>
 *
 * @author xiaojl
 * @since 2017-08-18
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
	private static Logger log = LoggerFactory.getLogger(SysRoleServiceImpl.class);
	
	@Autowired
	private SysRoleResourceService sysRoleResourceService;
	@Autowired
	private SysResourceService sysResourceService;
	
	@Override
	public Page<SysRole> selectPage(Page<SysRole> page, SysRole sysRole) {
		if(ToolUtil.isNotEmpty(sysRole)){
			return this.selectPage(page,sysRole.bindingQueryWrapper());
		}else{
			return this.selectPage(page);
		}
	}

	@Override
	public void deleteWithCascade(Long roleId) {
		List<SysRoleResource> rrlist = sysRoleResourceService.selectList(new EntityWrapper<SysRoleResource>().eq("roleId",roleId));
		if (ToolUtil.isNotEmpty(rrlist)) {
			log.info("有【" + rrlist.size() + "】个菜单关联到此角色，执行删除");
			sysRoleResourceService.delete(new EntityWrapper<SysRoleResource>().eq("roleId",roleId));
		}
		
		this.deleteById(roleId);
	}

	@Override
	public String selectResourcesTreeByAssign(String resourceIds) {
		List<SysResource> resList = sysResourceService.selectList(new EntityWrapper<SysResource>().orderBy("seq", true));
		StringBuffer strJson = new StringBuffer();
		strJson.append("<ul class=\"tree treeFolder treeCheck expand\" oncheck=\"selectResource\">");
		//strJson.append("<li><a >资源菜单</a>");
		recursionTreeMenu("0", strJson, resList,resourceIds);
		//strJson.append("</li>");
		strJson.append("</ul>");
		
		return strJson.toString();
	}
	
	/**
	 * 递归输出树形菜单
	 * 
	 * @param pId
	 * @param buffer
	 */
	private void recursionTreeMenu(String pId, StringBuffer buffer, List<SysResource> list, String resourceIds) {
		List<SysResource> listMap = getSonMenuListByPid(pId, list);
		if(ToolUtil.isNotEmpty(listMap)){
			//buffer.append("<ul>");
			for(SysResource res :listMap){
				if(resourceIds.contains(res.getId()+"")){
					buffer.append("<li><a tname=\"resourceName\" checked=\"true\" tvalue=\""+res.getId()+"\">"+res.getName()+"</a>");
				}else{
					buffer.append("<li><a tname=\"resourceName\" checked=\"false\" tvalue=\""+res.getId()+"\">"+res.getName()+"</a>");
				}
				StringBuffer sb = new StringBuffer("");
				recursionTreeMenu(res.getId()+"", sb, list, resourceIds);
				if(!"".equals(sb.toString())){
					buffer.append("<ul>");
					buffer.append(sb.toString());
					buffer.append("</ul>");
				}
				buffer.append("</li>");
			}
			//buffer.append("</ul>");
		}
		
	}
	
	/**
	 * 根据(pId)获取(menuList)中的所有子菜单集合.
	 * 
	 * @param pId
	 *            父菜单ID.
	 * @param menuList
	 *            菜单集合.
	 * @return sonMenuList.
	 */
	private List<SysResource> getSonMenuListByPid(String pId, List<SysResource> menuList) {
		List<SysResource> sonMenuList = new ArrayList<SysResource>();
		for (SysResource menu : menuList) {
			if (menu != null) {
				String parentId = menu.getPid()+"";// 父id
				if (parentId.equals(pId)) {
					sonMenuList.add(menu);
				}
			}
		}
		return sonMenuList;
	}
	
}
